// pages/ghpt/ghpt.js
Component({
  properties: {},
  data: {
    index: 0,
    arr:[], 
    array:[],
    zhuanjia: [],   
    mode: "scaleToFill",
    indicatorDots: false,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    date: '',
    datePickerValue: ['', '', '', ''],
    datePickerIsShow: false, 
    userInfo: '',
    hasUserInfo: false,   
    nickname:'',
    form_info:'',
    inputShowed:false,
    current:0,
    type:'',
    color:"#666",
    color1: "#666",
    color2: "#666",
    showDorctor:false,
    menzhen: ['青少年白癜风专病号', '肢端型白癜风专病号', '头面部白癜风专病号','进展期白癜风专病号'],
    mIndex:0,
    shijian: ['8：00-9：00', '9：00-10：00', '10：00-11：00', '11：00-12：00', '12：00-13：00', '13：00-14：00', '14：00-15：00', '15：00-16：00', '16：00-17：00', '17：00-18：00'],
    sindex: 0
  },
  methods:{
    // 轮播点的样式
    swiperChange:function(e){
      this.setData({
        current: e.detail.current
      })
    },
    //时间段
    bindPickerChange: function (e) {
      console.log('picker发送选择改变，携带值为' + this.data.shijian[e.detail.value])
      this.setData({
        sindex: e.detail.value,
        color2: '#333'
      })
    },
    // 门诊数据
    getMenzhen: function () {
      var that = this
      wx.request({
        url: 'https://huayan.jgyljt.com/hywuliao/zt/index.php?f=get&type=xcx&ua=bbmenzhen',
        data: {},
        method: 'GET',
        success: function (res) {
          var data = res.data['data'];
          var arr = ["请选择预约门诊"];
          var mzData = arr.concat(data)
          that.setData({
            menzhen: mzData
          })
        },
      })
    },
    // 获取数据
    getZhuanjia:function(){
      var that=this
      wx.request({
        url: 'https://huayan.jgyljt.com/hywuliao/zt/index.php?f=get&type=xcx&ua=sj',
        data: {},
        method: 'GET',
        success: function (res) { 
          var data = res.data.data;
          var array = ['请选择预约医生'], zhuanjia=[];
          for (var i in data){
            zhuanjia.push(data[i]['img']);
            array.push(data[i]['doctor']);
          }
          that.setData({
            zhuanjia,
            array
          })
        },
      })  
    },
    // 获取banner数据
    getBanner: function () {
      var that = this
      wx.request({
        url: 'https://huayan.jgyljt.com/hywuliao/banner/index.php?f=get&type=xcx&ua=sj',
        data: {},
        method: 'GET',
        success: function (res) {
          var data = res.data.data;
          var arr = []
          for (var i in data) {
            arr.push(data[i]['img']);
          }
          that.setData({
            arr
          })
        },
      })
    },
    radioChange(e) {
      this.setData({
        type: e.detail.value
      })
    },
    // 遍历专家
    bindDoctor: function (e) {
      this.setData({
        index: e.detail.value,
        color:'#333'
      })
    },
    bindMenzhen: function (e) {
      console.log(e.detail.value)
      this.setData({
        mIndex: e.detail.value,
        color1: '#333'
      })
    },
    // 显示时间插件
    showDatePicker: function (e) {
      this.setData({
        datePickerIsShow: true,
      });
    },
    // 时间插件确认
    datePickerOnSureClick: function (e) {
      this.setData({
        date: `${e.detail.value[0]}-${e.detail.value[1]}-${e.detail.value[2]}`,
        datePickerValue: e.detail.value,
        datePickerIsShow: false,
      });
    },
  // 时间插件取消
    datePickerOnCancelClick: function (event) {
      this.setData({
        datePickerIsShow: false,
      });
    },
    // 提交数据
    formSubmit: function (e) {      
     
      var that = this;
      var name = e.detail.value.name;
      var names = name.substr(0, 1) + new Array(name.length).join('*');
      var tel = e.detail.value.tell;
      var date = e.detail.value.date + ' ' + this.data.shijian[this.data.sindex];       
      var doctor = this.data.index == 0 ?'主任医师': this.data.array[this.data.index];
      var menzhen = this.data.menzhen[this.data.mIndex];
      var type=this.data.type;
      var num = date.replace(/[^0-9]/ig, "");
      var nums = parseInt(Math.random() * 999);
      var yyh = num + nums;     
      var wenti = "预约医生：" + doctor + "；是否确诊：" + type + ";预约门诊：" + menzhen;
      var click ="白斑小程序挂号";
     
      if (e.detail.value.name == "") {
        wx.showToast({
          title: '姓名不能为空',
          icon: 'none',
          duration: 2000,
          mask: true
        })
        return false;
      }
      if (e.detail.value.tell == "") {
        wx.showToast({
          title: '电话不能为空',
          icon: 'none',
          duration: 2000,
          mask: true
        })
        return false;
      }
      var phone = e.detail.value.tell
      if (!(/^1[3456789]\d{9}$/.test(phone))) {
        wx.showToast({
          title: '请正确填写您的手机号码！',
          icon: 'none',
          duration: 1500
        })
        return false;
      }     
      wx.showModal({
        title: '提示',
        content: '获取用户信息',
        success: function (res) {
          wx.getUserProfile({
            desc: '用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (res) => {
              console.log(res.userInfo.nickName)
              that.setData({
                nickname: res.userInfo.nickName,
                hasUserInfo: true
              })
            }            
          })
          wx.requestSubscribeMessage({
            tmplIds: ['orlu4AFwcyWeSs7vE1NsINEQAtYe8OH5JKgxo27uLH4'],
            success(res) {
              var formId = e.detail.formId;
              wx.showLoading({
                title: '提交中',
                mask: true
              })
              wx.login({
                success: res => {
                  var code = res.code;
                  var b = 'https://huayan.jgyljt.com/jgswt/sendTemplate2.php';
                  wx.request({
                    url: b,
                    method: "post",
                    header: {
                      'content-type': 'application/x-www-form-urlencoded'
                    },
                    data: {
                      name: e.detail.value.name,
                      tel: e.detail.value.tell,
                      date: date,
                      wenti: doctor,
                      menzhen: menzhen,
                      yuyue_num: yyh,
                      templateid: "orlu4AFwcyWeSs7vE1NsINEQAtYe8OH5JKgxo27uLH4",
                      code: code
                    },
                    success: function (res) {
                      wx.hideLoading()
                      wx.navigateTo({
                        url: '/pages/success/success?name=' + names + '&tel=' + tel + '&date=' + date
                          + '&type=' + type + '&wenti=' + doctor + '&click=' + click + '&menzhen=' + menzhen,
                      })
                    },
                    fail(res) {
                      wx.showToast({
                        title: '提交失败',
                        icon: 'none',
                        duration: 2000,
                        mask: true
                      })
                      console.log(res)
                    }
                  });
                  wx.request({
                    url: 'https://huayan.jgyljt.com/jgswt/msg.php',
                    data: {
                      name: name + '(' + that.data.nickname + ')',
                      tel: tel,
                      time: date,
                      wenti: wenti,
                      searchWord: "微信咨询",
                      click: click
                    },
                    method: 'GET',
                    success: function (res) { },
                  })

                },
              });
            }
          })
        }
      })
       
    },
    calling: function () {
      wx.makePhoneCall({
        phoneNumber: '0551-62623332',
        success: function () {
          console.log("拨打电话成功！")
        },
        fail: function () {
          console.log("拨打电话失败！")
        }
      })
    },
    guahao:function(e){
      this.setData({
        showDorctor:true,
        index: parseInt(e.currentTarget.dataset.index)+1
      });
      wx.pageScrollTo({
        scrollTop: 0
      })
      
    },
    zjinfo:function(){
      wx.pageScrollTo({
        scrollTop:880
      })
    },
    onShareAppMessage: function (ops) {
      return {
        title: '合肥华研白癜风防治所',
        path: '/pages/bbgh/bbgh',
        imageUrl: '',
        success: function (res) {
          // 转发成功
          console.log(res);
          console.log("转发成功:" + JSON.stringify(res));
        },
        fail: function (res) {
          // 转发失败
          console.log("转发失败:" + JSON.stringify(res));
        }
      }
    },
    onPullDownRefresh() {
      this.getMenzhen()
      this.getBanner()
      this.getZhuanjia()
    },
    onShow: function () {
      wx.hideHomeButton({
        success: function () {
          console.log(11)
        },
        fail: function () {
          console.log(22)
        }
      })
    } 
  },
  /**
   * 生命周期函数--监听页面加载
   */
  created: function (options) {
    this.getMenzhen()
    this.getBanner()
    this.getZhuanjia()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(11)
    
  }
})